package models

import (
	"gorm.io/gorm"
	"zk/zg3_zk2/src/pro/global"
)

type Videos struct {
	gorm.Model
	Title        string `gorm:"type:varchar(50);comment:'标题'"`
	Likes        int    `gorm:"type:int(5);comment:'点赞数'"`
	CommentCount int    `gorm:"type:int(5);comment:'评论数'"`
	Tag          string `gorm:"type:varchar(20);comment:'标签'"`
}

func (v *Videos) Search(page, pageSize int, keyword string) (listVideos []Videos, err error) {
	query := global.DB
	if keyword != "" {
		query = query.Where("title like ? OR tag like ?", "%"+keyword+"%", "%"+keyword+"%")
	}

	query = query.Scopes(Paginate(page, pageSize)).
		Find(&listVideos)

	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

// 视频播放记录表
type VideosVisitLog struct {
	gorm.Model
	UserId   int //用户ID
	VideosId int //视频ID
}
